import DBConf from '#Config/database.js'
import { dataGet } from '#Utils/helper.js'

/** @type {import('sequelize-cli').Migration} */
export default {
    async up(queryInterface, Sequelize) {
        /**
         * Add altering commands here.
         *
         * Example:
         * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
         */
        const currentDatabase = queryInterface.sequelize.config.database;
        console.log(queryInterface.sequelize.config);
        
        

        const publicDB = dataGet(DBConf, 'defaultDB');
        console.info('currentDatabase', currentDatabase)
        console.info('publicDB', publicDB)

        if (currentDatabase !== publicDB) {
            return
        }

        await queryInterface.createTable('users', {
            id: {
                type: Sequelize.INTEGER,
                primaryKey: true,
                autoIncrement: true,
            },
            createdAt: {
                type: Sequelize.DATE,
                allowNull: true,
                field: 'created_at',
            },
            updatedAt: {
                type: Sequelize.DATE,
                allowNull: true,
                field: 'updated_at',
            },
            deletedAt: {
                type: Sequelize.DATE,
                allowNull: true,
                field: 'deleted_at',
            },
            name: {
                type: Sequelize.STRING,
                allowNull: false,
                defaultValue: '',
            },
            username: {
                type: Sequelize.STRING,
                allowNull: false,
                defaultValue: '',
            },
            password: {
                type: Sequelize.STRING,
                allowNull: false,
                defaultValue: '',
            },
            email: {
                type: Sequelize.STRING,
                allowNull: false,
                unique: true,
                defaultValue: '',
            }
        }, {
            timestamps: true,
            paranoid: true,
        })
    },

    async down(queryInterface, Sequelize) {
        /**
         * Add reverting commands here.
         *
         * Example:
         * await queryInterface.dropTable('users');
         */
        const currentDatabase = queryInterface.sequelize.config.database;

        const publicDB = dataGet(DBConf, 'defaultDB');

        if (currentDatabase !== publicDB) {
            return
        }

        await queryInterface.dropTable('users')
    }
};
